package com.google.code.nexttraffic.web.action;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.google.code.nexttraffic.web.model.AreaInfoItem;
import com.google.code.nexttraffic.web.model.AreaInfoNaverItem;
import com.google.code.nexttraffic.web.openapi.impl.AreaInfoOpenApiNaverImpl;
import com.google.code.nexttraffic.web.openapi.impl.AreaInfoOpenApiYahooImpl;

public class AreaInfoAction {

	private AreaInfoItem areaInfoItem;
	
	private List<AreaInfoNaverItem> areaInfoNaverItem;

	// 야후 맵 키
	private final String YAHOO_MAP_KEY = "dasXcejV34Gl3o1vF_JLmdb7TfH9ZO5fbVIpCvSN1.iG2GgJjYrdKauT0szH7ZWxE88PFfn1Csp..uvT";

	// 다음 맵 키
	private final String NAVER_SEARCH_KEY = "ab46a1f5b917bff198e97e15c6968bfb";

	// 야후 주소
	private final String Y_RGC_URL = "http://kr.open.gugi.yahoo.com/service/poi.php";

	// 야후 주소
	private final String NAVER_SEARCH_URL = "http://openapi.naver.com/search";

	public String execute() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();

		// Request UTF-8로 인코딩
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		// 파라미터 세팅(문자열 또는 숫자로 변환)-유효성 검사
		String query = request.getParameter("query");
		String query_area = query.substring(0, 2) + " 맛집";
		String query_sleep = query.substring(0, 2) + " 숙박";

		try {
			query_area = URLEncoder.encode(query_area, "UTF-8");
			query_sleep = URLEncoder.encode(query_sleep, "UTF-8");
			query = URLEncoder.encode(query, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		// 네이버
		// 파라미터 만들기
		String params = "";
		params = "?appid=" + YAHOO_MAP_KEY;
		params += "&q=" + query;
		params += "&encoding=" + "utf-8";
		params += "&results=" + 100;
		params += "&output=" + "xml";

		// URL클래스를 통한 요청과 응답 송수신 변수들
		BufferedReader in = null;
		String inputLine = "";
		String buffer = "";

		try {
			URL openapi = null;

			openapi = new URL(Y_RGC_URL + params);

			in = new BufferedReader(new InputStreamReader(openapi.openStream(),
					"UTF-8"));

			while ((inputLine = in.readLine()) != null) {
				buffer += inputLine;
			}

			in.close();
		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			in = null;
		}

		System.out.println(buffer);

		AreaInfoOpenApiYahooImpl aioayhi = AreaInfoOpenApiYahooImpl
				.getInstance();

		aioayhi.xmlParser(new StringBuffer(buffer));
		AreaInfoItem aii = aioayhi.xmlItemParser();

		setAreaInfoItem(aii);

		// 네이버
		// 파라미터 만들기
		params = "";
		params = "?key=" + NAVER_SEARCH_KEY;
		params += "&query=" + query_area;
		params += "&target=" + "local";
		params += "&display=" + 50;
		params += "&start=" + 1;
		params += "&sort=" + "random";

		inputLine = "";
		buffer = "";

		try {
			URL openapi = null;

			openapi = new URL(NAVER_SEARCH_URL + params);

			in = new BufferedReader(new InputStreamReader(openapi.openStream(),
					"UTF-8"));

			while ((inputLine = in.readLine()) != null) {
				buffer += inputLine;
			}

			in.close();
		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			in = null;
		}

		AreaInfoOpenApiNaverImpl aioani = AreaInfoOpenApiNaverImpl.getInstance();

		aioani.xmlParser(new StringBuffer(buffer));
		areaInfoNaverItem= aioani.xmlItemsParser();
		
		request.setAttribute("naverfood", areaInfoNaverItem);
		
		
		// 네이버
		// 파라미터 만들기
		params = "";
		params = "?key=" + NAVER_SEARCH_KEY;
		params += "&query=" + query_sleep;
		params += "&target=" + "local";
		params += "&display=" + 50;
		params += "&start=" + 1;
		params += "&sort=" + "random";
		
		inputLine = "";
		buffer = "";
		
		try {
			URL openapi = null;
			
			openapi = new URL(NAVER_SEARCH_URL + params);
			
			in = new BufferedReader(new InputStreamReader(openapi.openStream(),
			"UTF-8"));
			
			while ((inputLine = in.readLine()) != null) {
				buffer += inputLine;
			}
			
			in.close();
		} catch (Exception e) {
			
			e.printStackTrace();
			
		} finally {
			in = null;
		}
		
		aioani.xmlParser(new StringBuffer(buffer));
		areaInfoNaverItem= aioani.xmlItemsParser();
		
		request.setAttribute("naversleep", areaInfoNaverItem);

		return "success";
	}

	public AreaInfoItem getAreaInfoItem() {
		return areaInfoItem;
	}

	public void setAreaInfoItem(AreaInfoItem areaInfoItem) {
		this.areaInfoItem = areaInfoItem;
	}

	public List<AreaInfoNaverItem> getAreaInfoNaverItem() {
		return areaInfoNaverItem;
	}

	public void setAreaInfoNaverItem(List<AreaInfoNaverItem> areaInfoNaverItem) {
		this.areaInfoNaverItem = areaInfoNaverItem;
	}
	
	

}
